package com.example.pestival11;
/*
import android.app.Activity;
import android.os.Bundle;

public class pestival4 extends Activity {
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pes4);
		
	}

}
*/
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class pestival4 extends ListActivity {

	Jason_Dataset jd;
	String w_title;
	String w_content;
	String w_pass;
	Activity table;
	LinearLayout tcon;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pes4);

						
				
		tcon = (LinearLayout) findViewById(R.id.icon1);
		// table.setListAdapter(new iconicProcess(table));
		table = this;
		SetList();

	}

	public void SetList() {
		jd = new Jason_Dataset();
		jd.Sel_all();
		((ListActivity) table).setListAdapter(new iconicProcess(table));
	}

	protected void onListItemClick(ListView l, View v,  int position,
			long id) { // ListView Click
		super.onListItemClick(l, v, position, id);
		
		Object o = this.getListAdapter().getItem(position);
		String developer = o.toString();
		
		
		 final LinearLayout linear = (LinearLayout) View.inflate(this,
				R.layout.pes5, null);
		 final EditText title_edit = (EditText) linear
					.findViewById(R.id.editText_title);
			final EditText content_edit = (EditText) linear
					.findViewById(R.id.editText_content);
			 
		 
			title_edit.setText(jd.title[position]);
			content_edit.setText(jd.content[position]);
			final String opass =jd.pass[position].toString();
			
		new AlertDialog.Builder(pestival4.this).setTitle("title")
				.setView(linear)
				.setPositiveButton("수정", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						
						EditText pass_edit = (EditText) linear
								.findViewById(R.id.editText_pass);
						
						final String npass =pass_edit.getText().toString();
						if(npass.equals(opass))//
						{
							String e_title = null;
							String e_content = null;
							String e_pass = null;
							
							try {
								e_title = URLEncoder.encode(title_edit.getText()
										.toString(), "euckr");
								e_content = URLEncoder.encode(content_edit
										.getText().toString(), "euckr");
								e_pass = URLEncoder.encode(pass_edit.getText()
										.toString(), "euckr");
							} catch (UnsupportedEncodingException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

							jd.insert_write(e_title, e_content, e_pass);

							SetList();
						}
						else
						Toast.makeText(pestival4.this,"鍮꾨�踰덊샇媛��ㅻ쫭�덈떎.", Toast.LENGTH_SHORT).show();
					}// onClick
				}// OnClickListener
				)// setPositiveButton
				.setNegativeButton("취소", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					//	Toast.makeText(AdapterTest3.this, "痍⑥냼�섏��듬땲��",
					//			Toast.LENGTH_SHORT).show();
					}
				}).show();

	}

	public void write_OnClick(View v) // 湲�벐湲�踰꾪듉
	{
		final LinearLayout linear = (LinearLayout) View.inflate(this,
				R.layout.pes5, null);
		new AlertDialog.Builder(pestival4.this).setTitle("글쓰기")
				.setView(linear)
				.setPositiveButton("저장", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						EditText title_edit = (EditText) linear
								.findViewById(R.id.editText_title);
						EditText content_edit = (EditText) linear
								.findViewById(R.id.editText_content);
						EditText pass_edit = (EditText) linear
								.findViewById(R.id.editText_pass);
						String e_title = null;
						String e_content = null;
						String e_pass = null;

						try {
							e_title = URLEncoder.encode(title_edit.getText()
									.toString(), "euckr");
							e_content = URLEncoder.encode(content_edit
									.getText().toString(), "euckr");
							e_pass = URLEncoder.encode(pass_edit.getText()
									.toString(), "euckr");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						jd.insert_write(e_title, e_content, e_pass);

						SetList();

					}// onClick
				}// OnClickListener
				)// setPositiveButton
				.setNegativeButton("취소", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					//	Toast.makeText(AdapterTest3.this, "痍⑥냼�섏��듬땲��",
						//		Toast.LENGTH_SHORT).show();
					}
				}).show();
	}

	class iconicProcess extends ArrayAdapter {

		Activity activity;

		public iconicProcess(Activity a) {
			super(a, R.layout.row, jd.title);
			this.activity = a;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			LayoutInflater inf = activity.getLayoutInflater();
			View row = inf.inflate(R.layout.row, null);

			//ImageView icon = (ImageView) row.findViewById(R.id.icon1);
			TextView text_title =(TextView) row.findViewById(R.id.text_title);
			TextView text_content = (TextView) row
					.findViewById(R.id.text_content);
			TextView text_index = (TextView) row.findViewById(R.id.text_index);
			text_title.setText("  제목 : "+jd.title[position]);
			text_content.setText("  내용 : "+jd.content[position]);
			// text_index.setText(" ["+jd.index[position]+"] ");
		//	icon.setImageResource(R.drawable.ic_launcher);

			return row;

		}

	}// Class iconicProcess

}
